package com.dfbz.dao;

import com.dfbz.common.Constr;
import com.dfbz.dao.base.BaseDao;
import com.dfbz.model.Article;
import com.dfbz.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao<Article> {
    public ArticleDao() {
        super( Article.class);
    }

    public long getArticleTotal(String searchName) {
        long total=0L;
        String sql = "select count(*) as totalArticle from article;";
        if (null!=searchName){
            sql = "select count(*) as totalArticle from article where title like ?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null!=searchName){
                map = qr.query(sql, new MapHandler(),"%"+searchName+"%");
            }else {
                map = qr.query(sql, new MapHandler());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null!=map){
            total = Long.parseLong(map.get("totalArticle").toString());
        }
        return total;
    }

    public static List<Map<String, Object>> getArticleList(int currentPage, String searchName) {
        String sql = "select a.*,(select count(*) from favorite where a_id = a.id) as totalFavorite\n" +
                "from article a\n" +
                "order by publish_date desc\n" +
                "limit ?,?";
        if (null!=searchName){
            sql="select a.*,(select count(*) from favorite where a_id = a.id) as totalFavorite\n" +
                    "from article a\n" +
                    "where title like ?\n" +
                    "order by publish_date desc \n" +
                    "limit ?,?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = null;
        try {
            if (null!=searchName){
                list = qr.query(sql, new MapListHandler(),"%"+searchName+"%", (currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }else {
                list = qr.query(sql, new MapListHandler(), (currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Map<String, Object> getArticleInfoById(String articleId) {
        String sql = "select a.*,(select count(*) from favorite where a_id = a.id) as totalFavorite\n" +
                "from article a\n" +
                "where id= ?";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String,Object> map = null;
        try {
            map = qr.query(sql,new MapHandler(),articleId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static List<Map<String, Object>> getArticleFocusUserList(String articleId) {
        String sql = "select ui.user_id,ui.real_name\n" +
                "from article a\n" +
                "join favorite f\n" +
                "join user_info ui\n" +
                "on a.id = f.a_id and f.u_id = ui.user_id\n" +
                "where a.id = ?";
        List<Map<String, Object>> list = null;
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            list = qr.query(sql, new MapListHandler(), articleId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Map<String, Object>> getCollectList(String userId, int currentPage, String searchName) {

        String sql = "select f.id as fid,a.*,(select count(*) from favorite where a_id = a.id) as totalFavorite\n" +
                "from favorite f\n" +
                "join article a on a.id=f.a_id\n" +
                "where u_id = ?\n" +
                "limit 0?,?";
        if (null!=searchName) {
            sql = "select f.id as fid,a.*,(select count(*) from favorite where a_id = a.id) as totalFavorite\n" +
                    "from favorite f\n" +
                    "join article a on a.id=f.a_id\n" +
                    "where u_id = ? and a.title like ?\n" +
                    "limit ?,?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = null;
        try {
            if (null!=searchName){
                list = qr.query(sql, new MapListHandler(),userId,"%"+searchName+"%", (currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }else {
                list = qr.query(sql, new MapListHandler(), userId,(currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static long getCollectTotal(String userId, String searchName) {
        long total=0L;
        String sql = "select count(*) as totalCollect \n" +
                "from favorite f \n" +
                "left join article a on f.a_id = a.id\n" +
                "where f.u_id = ?";
        if (null!=searchName){
            sql = "select count(*) as totalCollect \n" +
                    "from favorite f \n" +
                    "left join article a on f.a_id = a.id\n" +
                    "where f.u_id = ? and a.title like ? ";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null!=searchName){
                map = qr.query(sql, new MapHandler(),userId,"%"+searchName+"%");
            }else {
                map = qr.query(sql, new MapHandler(),userId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null!=map){
            total = Long.parseLong(map.get("totalCollect").toString());
        }
        return total;
    }
}
